package org.grottarossa.ui.core.components;

import org.grottarossa.data.Query;
import org.grottarossa.data.Query.ResultSetType;
import org.grottarossa.data.exception.QueryException;
import org.grottarossa.template.data.pojo.table.Table;
import org.grottarossa.ui.core.container.ListContainer;
import org.grottarossa.ui.core.container.TableContainer;


import com.vaadin.data.Container;
import com.vaadin.ui.ListSelect;

public class TMSListSelect extends ListSelect implements ITMSControl
{
	/**
	 *
	 */
	private static final long serialVersionUID = 5618339994817715788L;
	private Query query;

	public TMSListSelect(String errorMsg)
	{
		initialize(errorMsg);
	}

	public TMSListSelect(String caption, String errorMsg)
	{
		initialize(errorMsg);
		setCaption(caption);
	}

	private void initialize(String errorMsg) {
		setNullSelectionAllowed(false);
		setWidth("100%");
		setRequired(false);
		setRequiredError(errorMsg);
	}

	public void populate(Query query) throws QueryException
	{
		if(query == null)
			return;

		this.query = query;

		removeAllItems();

		Table data = (Table)query.execute(ResultSetType.KeyValue);

		TableContainer c = new TableContainer(this.query.getStoragePropertyName());
		c.setDataTable(data);

		this.setContainerDataSource(c);
	}

	public TMSListSelect attachDependentControl(ITMSControl dependentField,
			IRelatedDataSourceProvider provider)
	{
		return this;
	}

	public ITMSControl attachDependentControl(ITMSControl dependentField,
			String field) {
		// TODO Auto-generated method stub
		return null;
	}
}
